/**
 * Created by hugan on 2020/3/2.
 */
import "../common/plugins/vue/2.6.11.min.js"
import axios from "../common/axios.js"
import storageUtil from "../common/util/storageUtil.js";

{
  new Vue({
    el: '#app',
    data() {
      return {
        errMsg: '',
        appInfos: {
          version: "版本号",
          buildTime: "发布时间",
          systemInfo: {
            //0-否,1-是,255-未知
            isAcOnline: '',
            //[0~100]-正常值,255-未知
            batteryValue: '?',
            mute: false,
            //范围[0, 100]
            volume: 0,
            //锁屏时间
            lockPlanTime: '',
            //关机时间
            shutdownTime: '',
            //轮询任务
            enableKillWinUpdateTask: false,
          },
          // lstExternalApp: ["app1", "app2"],
          lstExternalApp: [],
        },
        lockPlanMinute: storageUtil.getObject('lockPlanMinute', 20),
        shutdownMinute: storageUtil.getObject('shutdownMinute', 20),
      }
    },
    watch: {
      lockPlanMinute(newValue) {
        storageUtil.setStore('lockPlanMinute', newValue)
      },
    },
    mounted() {
      //处理api错误消息
      axios.handlerErrMsg = err => this.errMsg = err
      axios.handleSuccess = resp => {
        this.errMsg = ''
        return resp
      }
      //
      this.getAppInfo()
    },
    methods: {
      //状态管理 ============
      getAppInfo() {
        axios.get("/api/appInfo").then(resp => {
          console.log("appInfo=", resp.data)
          let data = resp.data
          const acOnlineMap = {
            0: '否',
            1: '是',
            255: '未知',
          }
          data.systemInfo.isAcOnline = acOnlineMap[data.systemInfo.isAcOnline]
          if (255 === data.systemInfo.batteryValue) {
            data.systemInfo.batteryValue = '未知'
          }
          // data.systemInfo.lockPlanTime = '2023-01-01 09:22:00'//test
          this.appInfos = data
        })
      },
      setMute(isMute) {
        axios.get("/api/setMute/" + isMute)
      },
      setVolume() {
        this.appInfos.systemInfo.mute = false
        axios.get("/api/setVolume/" + this.appInfos.systemInfo.volume)
      },
      muteAndLock() {
        this.appInfos.systemInfo.mute = true
        axios.get("/api/muteAndLock")
      },
      lock() {
        axios.get("/api/lock")
      },
      lockPlan() {
        let minute = parseInt(this.lockPlanMinute)
        console.log("minute=" + minute)
        if (isNaN(minute) || minute <= 0) {
          alert("时间有误")
          return
        }
        axios.get("/api/lockPlan/" + minute).then(resp => {
          window.location.reload()
        })
      },
      lockPlanCancel() {
        axios.get("/api/lockPlanCancel").then(resp => {
          window.location.reload()
        })
      },
      //电源管理 ============
      sleep() {
        if (confirm("确认睡眠?")) axios.get("/api/sleep")
      },
      shutdownCancel() {
        axios.get("/api/shutdownCancel").then(resp => {
          window.location.reload()
        })
      },
      shutdown() {
        let minute = parseInt(this.shutdownMinute)
        console.log("minute=" + minute)
        if (isNaN(minute)) {
          alert("请输入时间")
          return
        } else if (minute <= 0) {
          if (!confirm("确认马上关机?")) return
          minute = 0
        }
        if (minute > 0) {
          //[马上关机]时 不保存
          storageUtil.setStore('shutdownMinute', minute)
        }
        axios.get("/api/shutdown/" + minute).then(resp => {
          window.location.reload()
        })
      },
      //程序 ============
      runExternalApp(name) {
        console.log("runExternalApp=" + name)
        if (confirm("运行[" + name + "]?")) axios.get("/api/runExternalApp/" + name)
      },
      switchKillWinUpdateTask() {
        let enable = !this.appInfos.systemInfo.enableKillWinUpdateTask
        axios.get("/api/setEnableKillWinUpdateTask/" + enable).then(resp => {
          window.location.reload()
        })
      },
      doExit() {
        console.log('doExit')
        if (confirm("确认关闭程序?")) {
          axios.get("/api/exit").then(resp => {
            this.errMsg = "已关闭"
          })
        }
      },
      doLogout() {
        if (confirm("确认登出?")) {
          window.location.href = './login.html'
        }
      },
    },
  })
}